package com.shouer.doc.ui.photopreview;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.FileUtil;
import com.shouer.doc.common.util.ScreenUtils;
import com.shouer.doc.widget.photodraweeview.PhotoDraweeView;

import java.io.File;
import java.util.List;

/**
 * Created by YYY on 2016/12/10.
 */

public class PhotoPreViewAdapter extends PagerAdapter {

    private List<String> photos;
    private ResizeOptions resizeOptions;
    private LayoutInflater layoutInflater;

    public PhotoPreViewAdapter(Context context, List<String> photos)
    {
        this.photos=photos;
        layoutInflater=LayoutInflater.from(context);
        int itemSize = ScreenUtils.getScreenWidth(context);
        int imgSize = itemSize * 2 / 3;
        resizeOptions = new ResizeOptions(itemSize, imgSize);
    }

    @Override
    public int getCount() {
        if(null==photos)
        {
            return 0;
        }else
        {
            return photos.size();
        }
    }

    @Override public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override public Object instantiateItem(ViewGroup viewGroup, int position) {

        String photo=photos.get(position);
        final PhotoDraweeView photoDraweeView = (PhotoDraweeView) layoutInflater.inflate(R.layout.activity_photo_review_item_layout,null);
        Uri imgUri=null;
        if (!TextUtils.isEmpty(photo)&&(photo.startsWith("http")||photo.startsWith("https")))
        {
            imgUri=Uri.parse(photo);
        }else
        {
            imgUri=Uri.parse(FileUtil.connectFilePath(ShouerConst.RES_SERVER,photo));
        }

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(imgUri).setResizeOptions(resizeOptions).setAutoRotateEnabled(true).build();

        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder().setOldController(photoDraweeView.getController())
                .setImageRequest(request)
                .setControllerListener(new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        super.onFinalImageSet(id, imageInfo, animatable);
                        if (imageInfo == null) {
                            return;
                        }
                        photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                    }
                })
                .build();
        photoDraweeView.setController(controller);

        try {
            viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return photoDraweeView;
    }
}
